Dino Geek, try to help you

What are the best optimization practices for `.htaccess`?


Optimizing the `.htaccess` file is essential for improving a website’s performance, security, and search engine optimization (SEO). Below are some of the best optimization practices:

  1. 1. Enable Gzip Compression
    Compressing files can reduce the amount of data that needs to be transferred between server and client, speeding up page load times.

```
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript

```
Source: [GTmetrix](https://gtmetrix.com/enable-gzip-compression.html)

  1. 2. Leverage Browser Caching
    Setting expiry dates for static resources so they can be cached by the user’s browser.

```
ExpiresActive On ExpiresByType text/html “access plus 1 month“ ExpiresByType image/gif “access plus 1 year“ ExpiresByType image/jpeg “access plus 1 year“ ExpiresByType image/png “access plus 1 year“ ExpiresByType text/css “access plus 1 month“ ExpiresByType text/javascript “access plus 1 month“ ExpiresByType application/javascript “access plus 1 month”

```
Source: [Google Developers](https://developers.google.com/speed/docs/insights/LeverageBrowserCaching)

  1. 3. Enable Keep-Alive
    Keep-Alive allows the server to maintain an open connection with the client, reducing latency.

```
Header set Connection keep-alive

```
Source: [Mozilla Developer Network (MDN)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive)

  1. 4. Set Custom 404 Error Page
    A custom 404 page can improve user experience when users encounter a broken link.

```
ErrorDocument 404 /custom_404.html
```
Source: [Apache HTTP Server Documentation](https://httpd.apache.org/docs/2.4/custom-error.html)

  1. 5. Implement Redirects Carefully
    Use 301 redirects for permanent redirects to preserve SEO ranking.

```
Redirect 301 /old-page.html /new-page.html
```
Source: [Moz](https://moz.com/learn/seo/redirection)

  1. 6. Block Unwanted Traffic
    Blocking certain IP addresses or user agents can prevent malicious access.

```
Order Allow,Deny Allow from all Deny from 123.45.6.7

RewriteEngine On RewriteCond %{HTTP_USER_AGENT} BadBot RewriteRule .* – [F,L]

```
Source: [Apache HTTP Server Documentation](https://httpd.apache.org/docs/2.4/howto/access.html)

  1. 7. Enhance Security
    Force HTTPS for secure communication and protect sensitive directories.

```
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Require all denied

```
Source: [OWASP](https://owasp.org/www-project-top-ten/)

  1. 8. Optimize Content Delivery
    Implementing Content Delivery Networks (CDN) and setting proper headers can improve load times.

```
Header set Cache-Control “public, max-age=31536000”

```
Source: [W3C](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)

  1. 9. Avoid Rewrites for Static Content
    Reduce unnecessary rewrites for static files like images, CSS, and JS.

```
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]

```
Source: [Apache HTTP Server Documentation](https://httpd.apache.org/docs/2.4/rewrite/remapping.html)

  1. 10. Use ETags Wisely
    ETags can help with smart caching but should be configured properly.

```
Header unset ETag

FileETag None
```
Source: [Yahoo Developer Network](https://developer.yahoo.com/performance/rules.html#etags)

By following these optimization practices, you can significantly enhance your website’s performance, security, and user experience. Each practice is backed by authoritative sources to ensure their reliability and effectiveness.

  1. Sources
    1. GTmetrix: https://gtmetrix.com/enable-gzip-compression.html
    2. Google Developers: https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
    3. Mozilla Developer Network (MDN): https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive
    4. Apache HTTP Server Documentation: https://httpd.apache.org/docs/2.4/custom-error.html
    5. Moz: https://moz.com/learn/seo/redirection
    6. OWASP: https://owasp.org/www-project-top-ten/
    7. W3C: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    8. Yahoo Developer Network: https://developer.yahoo.com/performance/rules.html#etags


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use